<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BPairObj
 *
 * @author ID59C
 */
class BPairEntity extends BEntity {

    public $mainObjId = null;
    public $pairObjId = null;
    public $pairObj2Id = null;
    public $relationshipId = null;
    public $resourceClass = null;
    public $userList = array();
    public $circleList = array();
    public $userId = 0;
    public $circleId = 0;
    public $spaceId = 0;
    public $appId = 0;
    protected $mainObj = null;
    protected $pairObj = null;
    protected $pairObj2 = null;
    protected $pairObjs = null;
    protected $relationship = null;

    public function getPropertyToColumnMapList($data = NULL) {
        $mapList = parent::getPropertyToColumnMapList($data);

        if ($this->mainObj instanceof BEntity && $this->pairObj instanceof BPermit && $this->pairObj2 instanceof BRole) {
            $mapList['mainObjId'] = 'resource_id';
            $mapList['pairObjId'] = 'permit_id';
            $mapList['pairObj2Id'] = 'role_id';
            $mapList['resourceClass'] = 'resource_class';
        } else {
            $mainObjClass = get_class($this->mainObj);
            $mainCol = strtolower(preg_replace('/^B/', '', $mainObjClass)) . "_id";
            //support group, for example, BAppGroup col is app_id and value is negative
            if ($this->mainObj instanceof BGroup)
                $mainCol = preg_replace('/Group/', '', $mainCol);

            if ($this->mainObj instanceof BApp || $this->mainObj instanceof BAppGroup)
                $mainCol = 'app_id';
            $mapList['mainObjId'] = $mainCol;

            $pairObjClass = get_class($this->pairObj);
            $pairCol = strtolower(preg_replace('/^B/', '', $pairObjClass)) . "_id";
            //support group, for example, BAppGroup col is app_id and value is negative
            if ($this->pairObj instanceof BGroup)
                $pairCol = preg_replace('/Group/', '', $pairCol);
            if ($this->pairObj instanceof BApp || $this->pairObj instanceof BAppGroup)
                $pairCol = 'app_id';
            $mapList['pairObjId'] = $pairCol;

            if ($this->pairObj2 != NULL) {
                $pairObj2Class = get_class($this->pairObj2);
                $pair2Col = strtolower(preg_replace('/^B/', '', $pairObj2Class)) . "_id";
                //support group, for example, BAppGroup col is app_id and value is negative
                if ($this->pairObj2 instanceof BGroup)
                    $pair2Col = preg_replace('/Group/', '', $pair2Col);
                if ($this->pairObj2 instanceof BApp || $this->pairObj2 instanceof BAppGroup)
                    $pair2Col = 'app_id';
                $mapList['pairObj2Id'] = $pair2Col;
            }
        }

        return $mapList;
    }

    public function getModelClass($data = NULL) {
        //parent::getModelClass($data);
        $mainObjClass = get_class($this->mainObj);
        $mClass = preg_replace("/^B/", "BM", $mainObjClass);
        //support group, for example, BSpaceGroup model class look like BMSpaceApp
        if ($this->mainObj instanceof BGroup)
            $mClass = preg_replace('/Group/', '', $mClass);
        if ($this->mainObj instanceof BApp || $this->mainObj instanceof BAppGroup) {
            $mClass = 'BMApp';
        }
        $pairObjClass = get_class($this->pairObj);
        if ($this->pairObj instanceof BApp || $this->pairObj instanceof BAppGroup)
            $mClass = $mClass . 'App';
        else {
            $mClass = $mClass . preg_replace("/^B/", '', $pairObjClass);
            //support group, for example, BSpaceGroup model class look like BMSpaceApp
            if ($this->pairObj instanceof BGroup)
                $mClass = preg_replace('/Group/', '', $mClass);
        }

        if ($this->pairObj2 != NULL) {
            $pairObj2Class = get_class($this->pairObj2);
            if ($this->pairObj2 instanceof BApp || $this->pairObj2 instanceof BAppGroup)
                $mClass = $mClass . 'App';
            else {
                $mClass = $mClass . preg_replace("/^B/", '', $pairObj2Class);
                //support group, for example, BSpaceGroup model class look like BMSpaceApp
                if ($this->pairObj2 instanceof BGroup)
                    $mClass = preg_replace('/Group/', '', $mClass);
            }

            if ($this->mainObj instanceof BEntity && $this->pairObj instanceof BPermit && $this->pairObj2 instanceof BRole)
                $mClass = 'BMResourcePermitRole';
        }

        return $mClass;
    }

    public function setMainObj($mainObj) {
        $this->mainObj = $mainObj;
        if ($mainObj instanceof BEntity)
            $this->mainObjId = $mainObj->getId();
        //for example for BAppGroup of notexistgroup, id set as -1
        if ($mainObj instanceof BGroup && preg_match('/Group/', get_class($mainObj)))
            $this->mainObjId = - $mainObj->getId();
    }

    public function setPairObjs($pairObjs) {
        $this->pairObjs = $pairObjs;
        if ($pairObjs instanceof BEntity) {
            $this->pairObj = $pairObjs;
            $this->pairObjId = $this->pairObj->getId();
            //for example for BAppGroup of notexistgroup, id set as -1
            if ($pairObjs instanceof BGroup && preg_match('/Group/', get_class($pairObjs)))
                $this->pairObjId = - $pairObjs->getId();
        }

        if (is_array($pairObjs)) {
            if (isset($pairObjs[0])) {
                $this->pairObj = $pairObjs[0];
                if ($this->pairObj instanceof BEntity)
                    $this->pairObjId = $this->pairObj->getId();
                if ($this->pairObj instanceof BGroup && preg_match('/Group/', get_class($this->pairObj)))
                    $this->pairObjId = - $this->pairObj->getId();
            }
            if (isset($pairObjs[1])) {
                $this->pairObj2 = $pairObjs[1];
                if ($this->pairObj2 instanceof BEntity)
                    $this->pairObj2Id = $this->pairObj2->getId();
                if ($this->pairObj2 instanceof BGroup && preg_match('/Group/', get_class($this->pairObj2)))
                    $this->pairObj2Id = - $this->pairObj2->getId();
            }
        }
    }

    public function setRelationship($relationship) {
        $this->relationship = $relationship;
        if ($relationship instanceof BRelationship)
            $this->relationshipId = $relationship->getId();
        if ($relationship instanceof BRelationshipGroup)
            $this->relationshipId = - $relationship->getId();
        if (is_numeric($relationship)) {
            $this->relationship = new BRelationship($relationship);
            $this->relationshipId = $relationship;
        }
    }

    public function getMainObj($context = NULL) {
        if ($this->mainObj instanceof BEntity && $this->mainObjId != NULL && $this->mainObj->getId() == NULL) {
            $class = get_class($this->mainObj);
            $id = $this->mainObjId;
            if ($id < 0)
                $id = - $id;
            $this->mainObj = new $class($id);
        }
        return $this->mainObj;
    }

    public function getPairObj($context = NULL) {
        if ($this->pairObj instanceof BEntity && $this->pairObjId != NULL && $this->pairObj->getId() == NULL) {
            $class = get_class($this->pairObj);
            $id = $this->pairObjId;
            if ($id < 0)
                $id = - $id;
            $this->pairObj = new $class($id);
        }
        return $this->pairObj;
    }

    public function getPairObj2($context = NULL) {
        if ($this->pairObj2 instanceof BEntity && $this->pairObj2Id != NULL && $this->pairObj2->getId() == NULL) {
            $class = get_class($this->pairObj2);
            $id = $this->pairObj2Id;
            if ($id < 0)
                $id = - $id;
            $this->pairObj2 = new $class($id);
        }
        return $this->pairObj2;
    }

    public function getRelationship($context = nul) {
        if ($this->relationship == NULL && $this->relationshipId != NULL) {
            $id = - $this->relationshipId;
            if ($id < 0) {
                $id = - $id;
                $this->relationship = new BRelationshipGroup($id);
            } else
                $this->relationship = new BRelationship($id);
        }
        return $this->relationship;
    }

    public function getPairObjs($context = NULL) {
        return $this->pairObjs;
    }

    /**
     * 
     * @param BRelationship $relationship
     */
    public function checkRelationship($relationship = NULL) {
        if ($this->relationshipId != NULL) {
            //self::findAll($context);
            $myRelationship = $this->getRelationship();
            if ($relationship instanceof BRelationship && $myRelationship instanceof BRelationship) {
                if ($relationship->getId() == $myRelationship->getId())
                    return TRUE;
            }
            if ($relationship instanceof BRelationshipGroup && $myRelationship instanceof BRelationshipGroup) {
                if ($relationship->isChildOf($myRelationship))
                    return TRUE;
            }
            if ($relationship instanceof BRelationship && $myRelationship instanceof BRelationshipGroup) {
                if ($myRelationship->haveMember($relationship))
                    return TRUE;
            }
        }
    }

    /**
     * 
     * @param type $context
     * @return BPairEntity
     */
    protected function getEntityList($context = NULL) {
        if ($context != NULL && is_array($context))
            $this->addFindCondition($context);
        return self::findAll($this);
    }

    public function getMainObjList($contex = NULL) {
        $entities = $this->getEntityList($contex);
        $mainObjList = array();
        $mainObjClass = get_class($this->mainObj);
        foreach ($entities as $entity) {
            if ($entity->mainObjId > 0) {
                if ($mainObjClass == 'BApp')
                    $mainObjList[$entity->mainObjId] = BApp::getAppById($entity->mainObjId);
                else
                    $mainObjList[$entity->mainObjId] = new $mainObjClass($entity->mainObjId);
                if ($entity->userId != 0)
                    $this->userList[$entity->mainObjId] = new BUser ($entity->userId);
                else {
                    $this->userList[$entity->mainObjId] = NULL;
                }
            }
            if ($entity->mainObjId < 0) {
                $mainObjGroupClass = get_class($entity->mainObj) . 'Group';
                $mainObjList[$entity->mainObjId] = new $mainObjGroupClass(-$entity->mainObjId);
                if ($entity->userId != 0)
                    $this->userList[$entity->mainObjId] = new BUser ($entity->userId);
                else {
                    $this->userList[$entity->mainObjId] = NULL;
                }
            }
        }
        return $mainObjList;
    }

    public function getPairObjList($context = NULL) {
        $entities = $this->getEntityList($context);
        $pairObjList = array();
        $pairObjClass = get_class($this->pairObj);
        foreach ($entities as $entity) {
            if ($entity->pairObjId > 0) {
                if ($pairObjClass == 'BApp')
                    $pairObjList[$entity->pairObjId] = BApp::getAppById($entity->pairObjId);
                else
                    $pairObjList[$entity->pairObjId] = new $pairObjClass($entity->pairObjId);
                if ($entity->userId != 0)
                    $this->userList[$entity->pairObjId] = new BUser ($entity->userId);
                else {
                    $this->userList[$entity->pairObjId] = NULL;
                }
            }
            if ($entity->pairObjId < 0) {
                //support group for data
                $pairObjGroupClass = get_class($entity->pairObj) . 'Group';
                $pairObjList[$entity->pairObjId] = new $pairObjGroupClass(-$entity->pairObjId);
                if ($entity->userId != 0)
                    $this->userList[$entity->pairObjId] = new BUser ($entity->userId);
                else {
                    $this->userList[$entity->pairObjId] = NULL;
                }
            }
        }
        return $pairObjList;
    }

    public function getUserList($context = NULL) {
        return $this->userList;
    }

    public function getPairObj2List($context = NULL) {
        $entities = $this->getEntityList($contex);
        $pairObj2List = array();
        $pairObj2Class = get_class($this->pairObj2);
        foreach ($entities as $entity) {
            if ($entity->pairObj2Id > 0) {
                if ($pairObj2Class == 'BApp')
                    $pairObjList[$entity->pairObj2Id] = BApp::getAppById($entity->pairObj2Id);
                else
                    $pairObjList[$entity->pairObj2Id] = new $pairObjClass($entity->pairObj2Id);
                if ($entity->userId != 0)
                    $this->userList[$entity->pairObj2Id] = new BUser ($entity->userId);
                else {
                    $this->userList[$entity->pairObj2Id] = NULL;
                }
            }
            if ($entity->pairObj2Id < 0) {
                $pairObjGroupClass = get_class($entity->pairObj2) . 'Group';
                $pairObj2List[$entity->pairObj2Id] = new $pairObjGroupClass(-$entity->pairObj2Id);
                if ($entity->userId != 0)
                    $this->userList[$entity->pairObj2Id] = new BUser ($entity->userId);
                else {
                    $this->userList[$entity->pairObj2Id] = NULL;
                }
            }
        }
        return $pairObj2List;
    }

    public function getRelationshipList($context = NULL) {
        $entities = $this->getEntityList($contex);
        $relationshipList = array();
        foreach ($entities as $entity) {
            if ($entity->relationshipId > 0)
                $relationshipList[$entity->relationshipId] = new BRelationship($entity->relationshipId);
            if ($entity->relationshipId < 0)
                $relationshipList[$entity->relationshipId] = new BRelationshipGroup(- $entity->relationshipId);
        }
        return $relationshipList;
    }

    /**
     * 
     * @param type $mainObj
     * @param type $pairObjs
     * @param type $relationship
     * @return \BPairEntity
     */
    public static function buildPairEntity($mainObj, $pairObjs, $relationship = NULL) {
        $pairEntity = new BPairEntity();
        $pairEntity->setMainObj($mainObj);
        $pairEntity->setPairObjs($pairObjs);
        $pairEntity->setRelationship($relationship);
        return $pairEntity;
    }

}
?>

